#include "DefaultCube.h"
#include <OgreRenderOperation.h>

DefaultCube::DefaultCube(const Ogre::String& name, float size, Ogre::ColourValue cubeColour): Ogre::ManualObject(name)
{
	float lSize = size;

	clear();
	begin("BaseWhiteNoLighting", Ogre::RenderOperation::OT_TRIANGLE_LIST);
	
	float cp = 1.0f * lSize ;
	float cm = -1.0f * lSize;

	position(cm, cp, cm);// a vertex
	colour(cubeColour);
	position(cp, cp, cm);// a vertex
	colour(cubeColour);
	position(cp, cm, cm);// a vertex
	colour(cubeColour);
	position(cm, cm, cm);// a vertex
	colour(cubeColour);
 
	position(cm, cp, cp);// a vertex
	colour(cubeColour);
	position(cp, cp, cp);// a vertex
	colour(cubeColour);
	position(cp, cm, cp);// a vertex
	colour(cubeColour);
	position(cm, cm, cp);// a vertex
	colour(cubeColour);

	triangle(0,1,2);
	triangle(2,3,0);
	triangle(4,6,5);
	triangle(6,4,7);

	triangle(0,4,5);
	triangle(5,1,0);
	triangle(2,6,7);
	triangle(7,3,2);

	triangle(0,7,4);
	triangle(7,0,3);
	triangle(1,5,6);
	triangle(6,2,1);	

	end();
}
 
DefaultCube::~DefaultCube()
{
}
 
DefaultRectangle::DefaultRectangle(const Ogre::String& name, Ogre::Vector3 dimensions, Ogre::ColourValue cubeColour): Ogre::ManualObject(name)
{

	clear();
	begin("FlatVertexColour", Ogre::RenderOperation::OT_TRIANGLE_LIST);
	
	float cp_x = 1.0f * dimensions.x;
	float cm_x = -1.0f * dimensions.x;

	float cp_y = 1.0f * dimensions.y;
	float cm_y = -1.0f * dimensions.y;

	float cp_z = 1.0f * dimensions.z;
	float cm_z = -1.0f * dimensions.z;

	Ogre::Vector3 A2 = Ogre::Vector3(cp_x, cp_y, cp_z);
	Ogre::Vector3 B2 = Ogre::Vector3(cm_x, cp_y, cp_z);
	Ogre::Vector3 C2 = Ogre::Vector3(cp_x, cm_y, cp_z);
	Ogre::Vector3 D2 = Ogre::Vector3(cm_x, cm_y, cp_z);
	
	Ogre::Vector3 A1 = Ogre::Vector3(cp_x, cp_y, cm_z);
	Ogre::Vector3 B1 = Ogre::Vector3(cm_x, cp_y, cm_z);
	Ogre::Vector3 C1 = Ogre::Vector3(cp_x, cm_y, cm_z);
	Ogre::Vector3 D1 = Ogre::Vector3(cm_x, cm_y, cm_z);
	 
	/*
	//TOP
	//Front Top Left
	position(cm_x, cp_y, cp_z);// a vertex
	colour(cubeColour);
	textureCoord(1,1);
	normal(0, 1, 0);
	//Back Top Left
	position(cm_x, cp_y, cm_z);// a vertex
	colour(cubeColour);
	textureCoord(1,1);
	normal(0, 1, 0);
	//Front Top Right
	position(cp_x, cp_y, cp_z);// a vertex
	colour(cubeColour);
	textureCoord(1,1);
	normal(0, 1, 0);
	//Back Top Right
	position(cp_x, cp_y, cm_z);// a vertex
	colour(cubeColour);
	textureCoord(1,1);
	normal(0, 1, 0);


	//BOTTOM
	//Front Bottom Left
	position(cm_x, cm_y, cp_z);// a vertex
	colour(cubeColour);
	normal(0, -1, 0);
	//Back Bottom Left
	position(cm_x, cm_y, cm_z);// a vertex
	colour(cubeColour);
	normal(0, -1, 0);
	//Front Bottom Right
	position(cp_x, cm_y, cp_z);// a vertex
	colour(cubeColour);
	normal(0, -1, 0);
	//Back Bottom Right
	position(cp_x, cm_y, cm_z);// a vertex
	colour(cubeColour);
	normal(0, -1, 0);


	//LEFT
	//Front Bottom Left
	position(cm_x, cm_y, cp_z);// a vertex
	colour(cubeColour);
	normal(-1, 0, 0);
	//Front Top Left
	position(cm_x, cp_y, cp_z);// a vertex
	colour(cubeColour);
	normal(-1, 0, 0);
	//Back Top Left
	position(cm_x, cp_y, cm_z);// a vertex
	colour(cubeColour);
	normal(-1, 0, 0);
	//Back Bottom Left
	position(cm_x, cm_y, cm_z);// a vertex
	colour(cubeColour);
	normal(-1, 0, 0);

	//RIGHT
	//Front Bottom Right
	position(cp_x, cm_y, cp_z);// a vertex
	colour(cubeColour);
	normal(1, 0, 0);
	//Front Top Right
	position(cp_x, cp_y, cp_z);// a vertex
	colour(cubeColour);
	normal(1, 0, 0);
	//Back Bottom Right
	position(cp_x, cm_y, cm_z);// a vertex
	colour(cubeColour);
	normal(1, 0, 0);
	//Back Top Right
	position(cp_x, cp_y, cm_z);// a vertex
	colour(cubeColour);
	normal(1, 0, 0);


	//FRONT
	//Front Bottom Left
	position(cm_x, cm_y, cp_z);// a vertex
	colour(cubeColour);
	normal(0, 0, 1);
	//Front Top Left
	position(cm_x, cp_y, cp_z);// a vertex
	colour(cubeColour);
	normal(0, 0, 1);
	//Front Bottom Right
	position(cp_x, cm_y, cp_z);// a vertex
	colour(cubeColour);
	normal(0, 0, 1);
	//Front Top Right
	position(cp_x, cp_y, cp_z);// a vertex
	colour(cubeColour);
	normal(0, 0, 1);

	//BACK
	//Back Bottom Left
	position(cm_x, cm_y, cm_z);// a vertex
	colour(cubeColour);
	normal(0, 0, -1);
	//Back Top Left
	position(cm_x, cp_y, cm_z);// a vertex
	colour(cubeColour);
	normal(0, 0, -1);
	//Back Bottom Right
	position(cp_x, cm_y, cm_z);// a vertex
	colour(cubeColour);
	normal(0, 0, -1);
	//Back Top Right
	position(cp_x, cp_y, cm_z);// a vertex
	colour(cubeColour);
	normal(0, 0, -1);

	//TOP
	triangle(0,1,2);
	triangle(3,2,1);
	
	//BOTTOM
	triangle(4,5,6);
	triangle(7,6,4);

	//LEFT
	triangle(8,9,10);
	triangle(10,11,8);


	//RIGHT
	triangle(13,12,14);
	triangle(15,13,12);

	//Front
	triangle(16,17,18);
	triangle(19,18,17);

	//Back
	triangle(20,21,22);
	triangle(23,22,21);	

	end();

	*/

// back side
      position( A2 ); normal( -( C2 - A2 ).crossProduct( ( D2 - A2 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour); // 0
      position( D2 ); normal( -( A2 - D2 ).crossProduct( ( C2 - D2 ) ).normalisedCopy()  ); textureCoord( 0, 1 ); colour(cubeColour);// 1
      position( C2 ); normal( -( D2 - C2 ).crossProduct( ( A2 - C2 ) ).normalisedCopy()  ); textureCoord( 1, 1 ); colour(cubeColour);// 2
      position( B2 ); normal( -( A2 - B2 ).crossProduct( ( D2 - B2 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 3

      // top side
      position( A1 ); normal( -( A2 - A1 ).crossProduct( ( B2 - A1 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour);// 4
      position( B2 ); normal( -( A1 - B2 ).crossProduct( ( A2 - B2 ) ).normalisedCopy()  ); textureCoord( 0, 1 ); colour(cubeColour);// 5
      position( A2 ); normal( -( B2 - A2 ).crossProduct( ( A1 - A2 ) ).normalisedCopy()  ); textureCoord( 1, 1 ); colour(cubeColour);// 6
      position( B1 ); normal( -( A1 - B1 ).crossProduct( ( B2 - B1 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 7

      // left side
      position( D1 );  normal( -( B2 - D1 ).crossProduct( ( D2 - D1 ) ).normalisedCopy()  ); textureCoord( 0, 1 ); colour(cubeColour);// 8
      position( D2 );  normal( -( D1 - D2 ).crossProduct( ( B2 - D2 ) ).normalisedCopy()  ); textureCoord( 1, 1 ); colour(cubeColour);// 9
      position( B2 );  normal( -( D2 - B2 ).crossProduct( ( D1 - B2 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour);// 10
      position( B1 );  normal( -( B1 - B2 ).crossProduct( ( D1 - B1 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour);// 11

      // right side
      position( A1 ); normal( -( C1 - A1 ).crossProduct( ( C2 - A1 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 12
      position( C2 ); normal( -( A1 - C2 ).crossProduct( ( C1 - C2 ) ).normalisedCopy()  ); textureCoord( 0, 1 ); colour(cubeColour);// 13
      position( C1 ); normal( -( C2 - C1 ).crossProduct( ( A1 - C1 ) ).normalisedCopy()  ); textureCoord( 1, 1 ); colour(cubeColour);// 14
      position( A2 ); normal( -( A1 - A2 ).crossProduct( ( C2 - A2 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 15

      // bottom
      position( C1 ); normal( -( D1 - C1 ).crossProduct( ( C2 - C1 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour);// 16
      position( D1 ); normal( -( C2 - D1 ).crossProduct( ( C1 - D1 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 17
      position( C2 ); normal( -( C1 - C2 ).crossProduct( ( D1 - C2 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour);// 18
      position( D2 ); normal( -( C2 - D2 ).crossProduct( ( D1 - D2 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 19

      // front side
      position( B1 ); normal( -( C2 - B1 ).crossProduct( ( D2 - B1 ) ).normalisedCopy()  ); textureCoord( 1, 0 ); colour(cubeColour);// 20
      position( C1 ); normal( -( B1 - C1 ).crossProduct( ( D2 - C1 ) ).normalisedCopy()  ); textureCoord( 0, 1 ); colour(cubeColour);// 21
      position( D1 ); normal( -( B1 - D1 ).crossProduct( ( C1 - D1 ) ).normalisedCopy()  ); textureCoord( 1, 1 ); colour(cubeColour);// 22
      position( A1 ); normal( -( D1 - A1 ).crossProduct( ( C1 - A1 ) ).normalisedCopy()  ); textureCoord( 0, 0 ); colour(cubeColour);// 23
       
      triangle( 0, 1, 2 );    triangle( 3, 1, 0 );   // back
      triangle( 4, 5, 6 );    triangle( 4, 7, 5 );   // top
      triangle( 8, 9, 10 );   triangle( 10, 11, 8 );   // left
      triangle( 12, 13, 14 ); triangle( 12, 15, 13 );   // right
      triangle( 18, 17, 16 ); triangle( 18, 19, 17 );   // bottom
      triangle( 20, 21, 22 ); triangle( 20, 23, 21 );   // front

      end();

}
 
DefaultRectangle::~DefaultRectangle()
{
}
